package com.rentalcar.rental_car.controller;

import com.rentalcar.rental_car.entity.News;
import com.rentalcar.rental_car.entity.Result;
import com.rentalcar.rental_car.service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@CrossOrigin
@RestController
@Slf4j
@RequestMapping("news")
public class NewsController {
    @Autowired
    NewsService newsService;
    @PostMapping("/newsList")
    public Result getNewsList(){
        try {
            log.info("查询所有的新闻");
            return Result.success(newsService.getNewsAll());
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
    }
    @PostMapping("/addNews")
    public Result addNews(@RequestBody News news){
        try {
            news.setDate(LocalDate.now());
            newsService.add(news);
            log.info("增加一条新闻");
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
    }
    @PostMapping("/deleteNews")
    public Result deleteNews(@RequestBody News news){
        try {
            log.info("删除一条新闻，编号为" + news.getNewsKey());
            newsService.delete(news);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
    }
}
